<template>
    <view class="bg-content">
        <div class="table table-bordered" style="text-align: center; background: white;">
            <div class="tr">
                <div class="td">
                    <div style="font-size: 20px; color: red; font-weight: bold;">
                        ￥{{statisticTotal.noWithdrawal | toFixed2}}
                    </div>
                    <div>未提现</div>
                </div>
                <div class="td">
                    <div style="font-size: 20px; color: green;">
                        ￥{{statisticTotal.yesWithdrawal | toFixed2}}
                    </div>
                    <div>已提现</div>
                </div>
            </div>
        </div>

        <u-sticky>
            <div style="border-bottom: 1rpx solid #eeeeee; height: 32px; background-color: #FFFFFF;">
                <u-search placeholder="提现批次号/提现流水号" v-model="searchKeyword" @search="onSearch" @custom="onSearch"></u-search>
            </div>
        </u-sticky>

        <scroll-view scroll-x="true">
            <div class="table table-bordered" style="width: 850px; text-align: center; background: white;">
                <div class="tr">
                    <div class="td" style="width: 150px;">店铺编号</div>
                    <div class="td" style="width: 100px;">推广收益</div>
                    <div class="td" style="width: 150px;">提现批次号</div>
                    <div class="td" style="width: 150px;">提现流水号</div>
                    <div class="td" style="width: 150px;">是否已经提现</div>
                    <div class="td" style="width: 150px;">创建时间</div>
                </div>
                <div class="tr" v-for="(item, index)  in tbDistributorMoneyList" :key="item.id">
                    <div class="td" style="width: 150px;">{{'item.nextTbStore.code' |toValue(item)}}</div>
                    <div class="td" style="width: 100px;">￥{{item.money | toFixed2}}</div>
                    <div class="td" style="width: 150px;">{{item.withdrawalCode ? item.withdrawalCode : '尚未提现无法产生'}}</div>
                    <div class="td" style="width: 150px;"> {{item.withdrawalTradeNo ? item.withdrawalTradeNo : '尚未提现无法产生'}}</div>
                    <div class="td" style="width: 150px;">{{item.isHaveWithdrawal ? '是' : '否'}}</div>
                    <div class="td" style="width: 150px;">{{item.createTime}}</div>
                </div>
            </div>
        </scroll-view>

        <div style="padding-top: 10px; padding-bottom: 10px;">
            <u-loadmore :status="pagination.status" bg-color="#eeeeee"/>
        </div>

        <div class="custom-bottom-button">
            <label @click="withdrawalSubmit()">
                提现
            </label>
        </div>
    </view>
</template>

<script>

    import {CommonUtil} from "@/dev/core/utils/common.util";
    import {TbDistributorMoneyService} from "@/dev/services/tb-distributor-money.service";

    export default {
        data() {
            return {
                searchKeyword: '',
                statisticTotal: 0,
                tbDistributorMoneyList: [],
            }
        },
        onShow() {
            const _self = this;
            _self.loadData();
        },
        // 上拉加载
        onReachBottom: function () {
            if (this.pagination.hasNextPage) {
                this.doInfinite()
            }
        },
        methods: {
            loadData() {
                const _self = this;
                new TbDistributorMoneyService().statisticTotal({
                    prevStoreId: _self.globalVariable.userInfo.tbStore.id,
                    type: '1'
                }).then(result => {
                    _self.statisticTotal = result;
                });

                _self.doRefresh(this.tbDistributorMoneyList)
            },
            gotoStore(item) {
                const _self = this;
                if (item.nextTbStore.communityId === _self.globalVariable.userInfo.tbStore.communityId) {
                    this.navigateTo(`/pages/common/store/index?storeId=${item.nextTbStore.id}`);
                } else {
                    CommonUtil.alert('该经销商与你处于不同的商城');
                }
            },
            onSearch() {
                const _self = this;
                this.searchKeyword = CommonUtil.trim(this.searchKeyword)
                _self.doRefresh(_self.tbDistributorMoneyList)
            },
            /**
             * 加载更多数据中
             * @param infiniteScroll
             */
            doInfinite(infiniteScroll) {
                const _self = this;
                new TbDistributorMoneyService().findTableListCascade({
                    pagination: _self.pagination,
                    data: {
                        prevStoreId: _self.globalVariable.userInfo.tbStore.id,
                        searchKeyword: _self.searchKeyword,
                        type: '1',  // 类型：0-推广收益，1-商城管理员收益
                        status: '1',
                        orderBy: 'create_time desc'
                    },
                }).then(result => {
                    _self.pagination.refreshPage(result);
                    _self.tbDistributorMoneyList = [..._self.tbDistributorMoneyList, ...result.list];
                });
            },
            withdrawalSubmit() {
                const _self = this;
                if (Number(_self.statisticTotal.noWithdrawal) < 0.1) {
                    CommonUtil.alert('未提现金额小于0.1元将无法提现');
                    return;
                }

                CommonUtil.alert({
                    title: '提示！',
                    subHeader: `请先在[卖家中心/店铺管理/店铺信息页面]核实店铺设置的支付宝账号和支付宝真实名字准确无误后再提现。`,
                    showCancel: true, // 是否显示取消按钮
                    success: function (res) {
                        if (res.cancel) { // 点击取消,默认隐藏弹框
                        } else { // 点击确定
                            new TbDistributorMoneyService().withdrawalToAccount({
                                prevStoreId: _self.globalVariable.userInfo.tbStore.id,
                                type: '1',  // 类型：0-推广收益，1-商城管理员收益
                            }).then(result => {
                                CommonUtil.alert(JSON.stringify(result.data));
                                _self.loadData();
                            });
                        }
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
